package main

import (
	"fmt"
	"net"
)

func main() {
	//listen, err := net.Listen("tcp", ":12345")
	tcpAddr, _ := net.ResolveTCPAddr("tcp", ":12345")
	listen, err := net.ListenTCP("tcp", tcpAddr)
	defer listen.Close()
	if err != nil {
		fmt.Printf("listen error : %v \n", err)
		return
	}
	for {
		//tcp 需要去accept，udp不需要
		conn, err := listen.Accept()
		if err != nil {
			fmt.Printf("connect error : %v \n", err)
			return
		}
		defer conn.Close()
		for {
			var buf = make([]byte, 512)
			n, err := conn.Read(buf)
			if err != nil { //关闭的时候出现EOF，必须加这一行
				fmt.Println(err)
				break
			}
			var wholeStr = string(buf[:n])
			fmt.Println("server received your message:" + wholeStr)
			conn.Write([]byte("server send back your message:" + wholeStr))
		}
	}
}
